package com.rerun.controller.base;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rerun.common.bean.Rest;
import com.rerun.entity.Province;
import com.rerun.entity.vo.ProvinceVo;
import com.rerun.service.ProvinceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * The type China city controller.
 */
@Api(value="省市区3级",description="省市区3级")
@RestController
@RequestMapping("/china")
public class ChinaCityController {
    /**
     * The Province service.
     */
    @Autowired
	ProvinceService provinceService;

    /**
     * Gets all provinces.
     *
     * @return the all provinces
     */
    @ApiOperation(value = "获取所有的省份", notes = "获取所有的省份", httpMethod = "GET")
	@RequestMapping(value="allProvinces")
	public Object getAllProvinces() {
		EntityWrapper<Province> wrapper = new EntityWrapper<>();
		List<Province> provices = provinceService.selectList(wrapper);
		List<ProvinceVo> provinceVos=new ArrayList<>();
		for (Province province:provices) {
			ProvinceVo vo=new ProvinceVo();
			vo.setId(province.getProvinceId());
			vo.setText(province.getProvinceName());
			 provinceVos.add(vo);
		}
		return Rest.okData(provinceVos);
	}

    /**
     * Gets list city.
     *
     * @param provinceId the province id
     * @return the list city
     */
    @ApiOperation(value = "根据省份ID查询城市", notes = "根据省份ID查询城市", httpMethod = "GET")
	@RequestMapping(value="getCity")
	public Object getListCity(@RequestParam String provinceId) {
		if(StringUtils.isNoneEmpty(provinceId)) {
			return Rest.okData(provinceService.getListCityByProvinceId(provinceId));
		}
		return Rest.failure("provinceId is null");
	}

    /**
     * Gets list dis.
     *
     * @param cityId the city id
     * @return the list dis
     */
    @ApiOperation(value = "根据城市ID查询地区", notes = "根据城市ID查询地区", httpMethod = "GET")
	@RequestMapping(value="getDistrict")
	public Object getListDis(@RequestParam String cityId) {
		if(StringUtils.isNoneEmpty(cityId)) {
			return Rest.okData(provinceService.getListDistrictByCityId(cityId));
		}
		return Rest.failure("cityId is null");
		
	}
}
